package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrs;
import com.atguigu.gulimall.product.vo.webvo.SkuItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrAttrgroupRelationDao relationDao;

    @Autowired
    private AttrAttrgroupRelationService relationService;
    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params, long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            //and方法
            wrapper.and((queryWrapper) -> {
                queryWrapper.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (catelogId == 0) {

            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", catelogId);

            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }

    }

    //删除 分组--属性  关联关系
    @Override
    public void removeAttrGroupRelation(AttrGroupVo[] attrGroupVos) {

        List<AttrAttrgroupRelationEntity> collect = Arrays.asList(attrGroupVos).stream().map((item) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        relationDao.deleteBatchRelation(collect);
    }

    // 获取属性分组没有关联的其他属性
    @Override
    public PageUtils getNoRelationAttr(Map<String, Object> params, Long attrgroupId) {
        //当前分组只能关联自己所属的分类里面的所有属性
        AttrGroupEntity attrGroupEntity = baseMapper.selectById(attrgroupId);
        Long catelogId = attrGroupEntity.getCatelogId();
        //2、当前分组只能关联别的分组没有引用的属性
        //当前分类下的所有分组
        List<AttrGroupEntity> group = baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<Long> collect = group.stream().map(item -> {
            return item.getAttrGroupId();
        }).collect(Collectors.toList());
        //这些分组关联的属性
        List<AttrAttrgroupRelationEntity> attr_group = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", collect));
        List<Long> attrIds = attr_group.stream().map(item -> {
            return item.getAttrId();
        }).collect(Collectors.toList());
        //从当前分类的所有属性中移除这些属性

        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>().eq("catelog_id", catelogId).eq("attr_type", 1);
        if (attrIds != null && attrIds.size() > 0) {
            wrapper.notIn("attr_id", attrIds);
        }
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.and(w -> {
                w.eq("attr_id", key).or().like("attr_name", key);
            });
        }
        IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params), wrapper);


        return new PageUtils(page);
    }

    //添加属性与分组关联关系
    @Override
    public void addAttr_GroupRelation(List<AttrGroupVo> attrGroupVo) {
        List<AttrAttrgroupRelationEntity> collect = attrGroupVo.stream().map(item -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        relationService.saveBatch(collect);

    }

    //获取分类下所有分组&关联属性
    @Override
    public List<AttrGroupWithAttrs> getAttrGroupWithAttrs(Long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper();
        wrapper.eq("catelog_id", catelogId);
        List<AttrGroupEntity> attrGroups = baseMapper.selectList(wrapper);
        List<AttrGroupWithAttrs> collect = attrGroups.stream().map(item -> {
            AttrGroupWithAttrs attrGroupWithAttrs = new AttrGroupWithAttrs();
            BeanUtils.copyProperties(item, attrGroupWithAttrs);
            List<AttrEntity> relationAttrs = attrService.getRelationAttr(item.getAttrGroupId());
            attrGroupWithAttrs.setAttrs(relationAttrs);
            return attrGroupWithAttrs;
        }).collect(Collectors.toList());
        return collect;
    }
    //spu规格包装pms_product_attr_value
    @Override
    public List<SkuItemVo.SpuItemAttrGroupVo> selectattrGroupVos(Long spuId, Long catalogId) {
        List<SkuItemVo.SpuItemAttrGroupVo> attrGroupVos=   baseMapper.selectattrGroupVos(spuId,catalogId);
        return attrGroupVos;
    }
}